/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package util;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Ferry
 */
public class RescaleImage {
    public RescaleImage(){

    }

    public Image rescaling(Image image, int width, int height)
	{
		int sourceWidth = image.getWidth();
		int sourceHeight = image.getHeight();

		Image newImage = Image.createImage(width, height);
		Graphics g = newImage.getGraphics();

		for(int y=0; y<height; y++)
		{
			for(int x=0; x<width; x++)
			{
				g.setClip(x, y, 1, 1);
				int dx = x * sourceWidth / width;
				int dy = y * sourceHeight / height;
				g.drawImage(image, x-dx, y-dy, Graphics.LEFT | Graphics.TOP);
			}
		}

		return Image.createImage(newImage);
	}
}
